package com.nondo.dean.auth.controller.api;

import com.nondo.dean.auth.common.bean.RoleAuthorityItem;
import com.nondo.dean.auth.common.bean.RoleItem;
import com.nondo.dean.auth.common.bean.RoleMenuItem;
import com.nondo.dean.auth.domain.Role;
import com.nondo.dean.auth.domain.RoleAuthorityKey;
import com.nondo.dean.auth.domain.RoleMenuKey;
import com.nondo.dean.auth.provider.RoleManager;
import com.nondo.dean.auth.service.RoleAuthorityService;
import com.nondo.dean.auth.service.RoleMenuService;
import com.nondo.dean.common.annotation.ApiVersion;
import com.nondo.dean.common.web.Page;
import com.nondo.dean.common.web.Query;
import com.nondo.dean.common.web.View;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 角色对外接口
 *
 * @author linfeng
 */
@RestController
@RequestMapping("/{v}/role")
public class RoleController {

    @Autowired
    private RoleManager roleManager;
    @Autowired
    private RoleMenuService roleMenuService;
    @Autowired
    private RoleAuthorityService roleAuthorityService;

    /**
     * 角色注册接口
     *
     * @param roleItem 角色参数
     * @return 角色注册消息
     */
    @ApiVersion(1)
    @RequestMapping(value = "/register", method = RequestMethod.POST)
    public View<String> requestRoleRegister(@Validated @RequestBody RoleItem roleItem) {

        return roleManager.registerRole(roleItem);
    }

    /**
     * 角色删除
     *
     * @param roleId 角色key
     * @return 删除消息
     */
    @ApiVersion(1)
    @RequestMapping(value = "/delete/{roleId}", method = RequestMethod.POST)
    public View<String> requestRoleDelete(@PathVariable Long roleId) {

        return roleManager.deleteRole(roleId);
    }

    /**
     * 角色菜单
     *
     * @param roleId 角色key
     * @return 删除消息
     */
    @ApiVersion(1)
    @RequestMapping(value = "/menu/{roleId}", method = RequestMethod.POST)
    public View<List<RoleMenuKey>> requestRoleMenu(@PathVariable Long roleId) {

        List<RoleMenuKey> roleMenuKeys = roleMenuService.findByRoleId(roleId);
        return View.ofOk(roleMenuKeys);
    }

    /**
     * 角色权限
     *
     * @param roleId 角色key
     * @return 删除消息
     */
    @ApiVersion(1)
    @RequestMapping(value = "/authority/{roleId}", method = RequestMethod.POST)
    public View<List<RoleAuthorityKey>> requestRoleAuthority(@PathVariable Long roleId) {

        List<RoleAuthorityKey> roleAuthorityKeys = roleAuthorityService.findByRoleId(roleId);
        return View.ofOk(roleAuthorityKeys);
    }

    /**
     * 角色权限绑定
     *
     * @param roleAuthorityItem 角色权限参数
     * @return 绑定消息
     */
    @ApiVersion(1)
    @RequestMapping(value = "/authority/bind", method = RequestMethod.POST)
    public View<String> requestAuthorityBind(@RequestBody RoleAuthorityItem roleAuthorityItem) {

        return roleManager.requestRoleAuthorityBind(roleAuthorityItem);
    }

    /**
     * 角色菜单绑定
     *
     * @param roleMenuItem 角色菜单参数
     * @return 绑定消息
     */
    @ApiVersion(1)
    @RequestMapping(value = "/menu/bind", method = RequestMethod.POST)
    public View<String> requestMenuBind(@RequestBody RoleMenuItem roleMenuItem) {

        return roleManager.requestRoleMenuBind(roleMenuItem);
    }

    /**
     * 分页查询角色
     *
     * @param query 分页参数
     * @return 查询结果
     */
    @ApiVersion(1)
    @RequestMapping(value = "/list", method = RequestMethod.POST)
    public View<Page<Role>> requestPageRole(@Validated @RequestBody Query<RoleItem> query) {
        return roleManager.findRolePage(query);
    }

    /**
     * 分页查询角色
     *
     * @param code 分页参数
     * @return 查询结果
     */
    @ApiVersion(1)
    @RequestMapping(value = "/role/code/{code}", method = RequestMethod.POST)
    public View<Long> requestRole(@PathVariable String code) {
        return roleManager.findRoleByCode(code);
    }

}
